package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;

	@Autowired
	private UserAccountDetailMapper userAccountDetailMapper;



	//进行账户的初始化
	@Override
	public void initUserAccount(Long userId) {
		UserAccount userAccount = new UserAccount();
		userAccount.setUserId(userId);
		userAccountMapper.insert(userAccount);
	}

	//获取账户可用余额
	@Override
	public BigDecimal getAvailableAmount(Long userId) {
		LambdaQueryWrapper<UserAccount> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(UserAccount::getUserId,userId);
		UserAccount userAccount = userAccountMapper.selectOne(wrapper);
		BigDecimal availableAmount = userAccount.getAvailableAmount();
//		UserAccount userAccount = this.getOne(new LambdaQueryWrapper<UserAccount>()
//				.eq(UserAccount::getUserId, userId));
//		return userAccount.getAvailableAmount();
		return availableAmount;
	}

	//检查并扣减余额
	@Override
	public void CheckAndDeduct(AccountLockVo accountLockVo) {
		//判断用户的账户余额是否大于要购买的商品的金额
		//获得购买的商品金额
		BigDecimal amount = accountLockVo.getAmount();
		//获取用户账户金额
		Long userId = accountLockVo.getUserId();
		UserAccount userAccount = userAccountMapper
				.selectOne(new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId, userId));

		BigDecimal availableAmount = userAccount.getAvailableAmount();
		if (availableAmount.intValue()>amount.intValue()){
			//表示余额充足
			//扣减余额
		BigDecimal	availableAmount_new = availableAmount.subtract(amount);
			BigDecimal totalAmount = userAccount.getTotalAmount();
			totalAmount = totalAmount.subtract(amount);
			userAccount.setTotalAmount(totalAmount);
			userAccount.setAvailableAmount(availableAmount_new);
			userAccount.setTotalPayAmount(userAccount.getTotalPayAmount().add(amount));
			userAccountMapper.updateById(userAccount);
			//添加余额支付记录  user_account_detail
			UserAccountDetail userAccountDetail = new UserAccountDetail();
			userAccountDetail.setUserId(userId);
			userAccountDetail.setAmount(amount);
			userAccountDetail.setTitle(accountLockVo.getContent());
			userAccountDetail.setOrderNo(accountLockVo.getOrderNo());
			userAccountDetail.setTradeType(SystemConstant.ACCOUNT_TRADE_TYPE_MINUS);
			userAccountDetailMapper.insert(userAccountDetail);

		}else{
			//余额不足
			throw new GuiguException(400,"账户余额不足！");
		}

	}

}
